/* UCR weather policy merge .do file
files used: UCR_1970_2015.dta, US_weather_1959_2017_daily.dta, cc_policy_month.dta
files created: NIBRS_weather_policy.dta
programs used: merge
notes: merge on state date
*/
********************************************************************************
// merge UCR data with weather and policy data

*bring in UCR data
use "Processed Data/UCR_1991_2016.dta", clear

*merge with weather data
merge m:1 fips month year using "Processed Data/US_weather_1991_2016_monthly.dta"
keep if _merge==3
drop _merge

*merge with concealed carry policy data
merge m:1 state month year using "Processed Data/monthly_gun_policies_UCR"
keep if _merge==3
drop _merge

*************************
*    Restrict sample
*************************

** Restrict the dataset to those that report monthly
drop if population == 0
egen tag = tag(ori9 year month)
egen distinct = total(tag), by(ori9 year)
keep if distinct == 12

**********************************
*    Create analysis variables
**********************************

** Group state
rename state statename
egen STATE = group(statename)

** RTC variables

*More/less prohibited
gen more_prohibited = 0 if unrestricted == 1 | shall_issue == 1
replace more_prohibited = 1 if may_issue == 1 | prohibited == 1
label define binary1 0 "Less Prohibited (Unrestricted, Shall issue)" 1 "More Prohibited (May issue, Prohibited)"
label values more_prohibited binary1

gen less_prohibited = 1 if unrestricted == 1 | shall_issue == 1
replace less_prohibited = 0 if may_issue == 1 | prohibited == 1
label define binary2 1 "Less Prohibited (Unrestricted, Shall issue)" 0 "More Prohibited (May issue, Prohibited)"
label values less_prohibited binary2

*bys STATE year: egen max_MP = max(more_prohibited)

gen MP = 0
replace MP = 1 if more_prohibited == 1

*drop temp_MP prec_MP

gen temp_MP = tMean*MP

gen prec_MP = prec*MP

** Create homicide outcome variable
rename murder_ homicide

gen homicide_pc = homicide/population
replace homicide_pc = homicide_pc*100000

sum homicide_pc if homicide_pc > 0, d

gen homicide_pc_trimmed = homicide_pc 

replace homicide_pc_trimmed = . if homicide_pc > `r(p99)'

egen tag_trim = tag(ori9 year month) if homicide_pc_trimmed != .
egen distinct_trim = total(tag_trim), by(ori9 year)
replace homicide_pc_trimmed = . if distinct_trim != 12

**********************************
*    Create fixed effects
**********************************

egen ORI = group(ori9)

egen sample_month = group(year month)
** State by year fe

egen state_year = group(STATE year)
egen state_month = group(STATE sample_month)

** ORI by year fe
egen ori_year = group(ori9 year)

*****
** NIBRS Jurisdictions 
*****

merge m:1 ori9 using "Processed Data/NIBRS_ORIs.dta"
keep if _m !=2
gen NIBRS = 0
replace NIBRS = 1 if _m == 3
drop _m

*save
saveold "Analysis Data/UCR_analysis.dta", replace version(12)

